package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.system.bean.Grid;
import com.srit.system.dao.GridDao;

/**
 * @author guan
 * 
 */
@Service
public class GridDaoImpl implements GridDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(Grid grid) {
		String sql = "insert into TC_GRID"
				+ " (ID,CONFIGID,NAME,HTMLID,HTMLNAME,GRIDTYPE,EFFECTTIME,SUBJECTTYPE,CODETYPE,CODEID)"
				+ " values(SEQ_TC_GRID.nextval,?,?,?,?,?,?,?,?,?)";
		jdbcTemplate.update(
				sql,
				new Object[] { grid.getConfigId(), grid.getName(),
						grid.getHtmlId(), grid.getHtmlName(),
						grid.getGridType(), grid.getEffectTime(),
						grid.getSubjectType(), grid.getCodeType(),
						grid.getCodeId() });
	}

	@Override
	public void update(Grid grid) {
		String sql = "update TC_GRID set CONFIGID=?,NAME=?,HTMLID=?,HTMLNAME=?,"
				+ "GRIDTYPE=?,EFFECTTIME=?,SUBJECTTYPE=?,CODETYPE=?,CODEID=? where id=?";
		jdbcTemplate.update(
				sql,
				new Object[] { grid.getConfigId(), grid.getName(),
						grid.getHtmlId(), grid.getHtmlName(),
						grid.getGridType(), grid.getEffectTime(),
						grid.getSubjectType(), grid.getCodeType(),
						grid.getCodeId(), grid.getId() });

	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	@Override
	public Grid getById(Integer id) {
		String sql = "select * from TC_GRID where id=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new GridRowMapper());
	}

	@Override
	public int getMaxId() {
		String sql = "select  max(ID) from TC_GRID";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<Grid> getListByConfigId(int configId) {
		String sql = "select * from TC_GRID where CONFIGID=?";
		return jdbcTemplate.query(sql, new Object[] { configId },
				new GridRowMapper());
	}

	@Override
	public void page(Page page) {
		// TODO Auto-generated method stub

	}

}

class GridResultSetExtractor implements ResultSetExtractor {
	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		Grid grid = new Grid();
		// ID,CONFIGID,NAME,HTMLID,HTMLNAME,GRIDTYPE,EFFECTTIME,SUBJECTTYPE,CODETYPE,CODEID
		grid.setId(rs.getInt("ID"));
		grid.setConfigId(rs.getInt("CONFIGID"));
		grid.setName(rs.getString("NAME"));
		grid.setHtmlId(rs.getString("HTMLID"));
		grid.setHtmlName(rs.getString("HTMLNAME"));
		grid.setGridType(rs.getString("GRIDTYPE"));
		grid.setEffectTime(rs.getInt("EFFECTTIME"));
		grid.setSubjectType(rs.getString("SUBJECTTYPE"));
		grid.setCodeType(rs.getString("CODETYPE"));
		grid.setCodeId(rs.getString("CODEID"));
		return grid;
	}

}

class GridRowMapper implements RowMapper {

	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		GridResultSetExtractor extractor = new GridResultSetExtractor();
		return extractor.extractData(rs);
	}

}
